<template>
  <view>
    <c-navBar :title="plan_sn ? '修改排课' : '排课新增'"></c-navBar>
    <view>
      <CForm
        :formRules="formRules"
        v-model:formData="formData"
        :formList="listForm"
        @submit="submit"
        @selectChange="selectChange"
      ></CForm>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue"
import CForm from "@/components/CForm/CForm.vue"
import { onLoad, onUnload, onBackPress } from "@dcloudio/uni-app"
import { addSchedulingForm } from "./data"
import {
  addPlan,
  savePlan,
  getPlanDetail,
  getSchoolTeacherList,
  getSchoolUserList
} from "@/apis/coachManagement"
import { getSchoolList } from "@/apis/school"
import {
  getAllCategoryType,
  getAllFirstCategory,
  getAllSecondCategory,
  getAllVersion
} from "@/apis/dict"
const { formField, formList, formRules } = addSchedulingForm
const listForm = ref(formList)
const defaultData = JSON.parse(JSON.stringify(formField))
let formData = ref(JSON.parse(JSON.stringify(formField)))
let plan_sn = ref("")
onBackPress(() => {
  formData.value = JSON.parse(JSON.stringify(defaultData))
})
onLoad((options) => {
  plan_sn.value = options.plan_sn
  getAllSchool()
  getVersionList()
  getCategoryTypeList()
  if (plan_sn.value) {
    getDetail()
  }
})
const getAllSchool = () => {
  getSchoolList().then((res) => {
    listForm.value[0].options = res.data.map((item) => {
      return {
        text: item.school_name,
        value: item.school_id
      }
    })
  })
}
const getDetail = () => {
  getPlanDetail({ plan_sn: plan_sn.value }).then((res) => {
    if (res.code === 1) {
      formData.value.school_id = res.data.school_id
      formData.value.day_time = res.data.day_time
      formData.value.teacher_id = res.data.teacher.id
      formData.value.user_data = res.data.teacher_plan_detail.map(
        (v) => v.user_id
      )
      formData.value.plan_type = res.data.plan_type
      formData.value.category_type = res.data.category_type
      formData.value.first_category_id = res.data.first_category_id
      formData.value.second_category_id = res.data.second_category_id
      formData.value.version_id = res.data.version_id
      formData.value.timeRange = [res.data.start_time, res.data.end_time]
      console.log(formData.value)

      if (formData.value.school_id) {
        getAllTeacherBySchoolId(formData.value.school_id)
        getAllStuBySchoolId(formData.value.school_id)
      }
      if (formData.value.category_type) {
        getFirstCategoryList()
      }
      if (formData.value.first_category_id) {
        getSecondCategoryList()
      }
    }
  })
}
const selectChange = ({ value, field }) => {
  console.log(value, field)
  if (field === "school_id") {
    getAllTeacherBySchoolId(value.value)
    getAllStuBySchoolId(value.value)
  } else if (field === "category_type") {
    getFirstCategoryList()
  } else if (field === "first_category_id") {
    getSecondCategoryList()
  }
}
const getVersionList = () => {
  getAllVersion().then((res) => {
    listForm.value[9].options = res.data.map((item) => {
      return {
        text: item.version_name,
        value: item.version_id
      }
    })
  })
}
const getAllTeacherBySchoolId = (school_id) => {
  getSchoolTeacherList({ school_id }).then((res) => {
    listForm.value[3].options = res.data.map((item) => {
      return {
        text: item.teacher_nickname,
        value: item.teacher_id
      }
    })
  })
}

const getAllStuBySchoolId = (school_id) => {
  getSchoolUserList({ school_id }).then((res) => {
    listForm.value[4].options = res.data.map((item) => {
      return {
        text: item.username,
        value: item.user_id
      }
    })
  })
}

const getCategoryTypeList = () => {
  getAllCategoryType().then((res) => {
    listForm.value[6].options = res.data.map((item) => {
      return {
        text: item.type_name,
        value: item.type_id
      }
    })
  })
}
const getFirstCategoryList = () => {
  getAllFirstCategory({ type_id: formData.value.category_type }).then((res) => {
    listForm.value[7].options = res.data.map((item) => {
      return {
        text: item.first_category_name,
        value: item.first_category_id
      }
    })
  })
}
const getSecondCategoryList = () => {
  getAllSecondCategory({
    type_id: formData.value.category_type,
    first_category_id: formData.value.first_category_id
  }).then((res) => {
    listForm.value[8].options = res.data.map((item) => {
      return {
        text: item.second_category_name,
        value: item.second_category_id
      }
    })
  })
}
const submit = () => {
  if (plan_sn.value) {
    edit()
  } else {
    add()
  }
}

const add = () => {
  console.log(formData.value)
  addPlan({
    ...formData.value,
    user_data: JSON.stringify(formData.value.user_data),
    start_time: formData.value.timeRange[0],
    end_time: formData.value.timeRange[1]
  }).then((res) => {
    if (res.code === 1) {
      uni.showSuccessToast("添加成功！")
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        })
      }, 1500)
    }
  })
}

const edit = () => {
  savePlan({
    ...formData.value,
    user_data: JSON.stringify(formData.value.user_data),
    plan_sn: plan_sn.value,
    start_time: formData.value.timeRange[0],
    end_time: formData.value.timeRange[1]
  }).then((res) => {
    if (res.code === 1) {
      uni.showSuccessToast("修改成功！")
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        })
      }, 1500)
    }
  })
}
</script>

<style lang="scss"></style>
